package com.oracle.sbdb.bo;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.oracle.sbdb.dao.UpdatesDao;
import com.oracle.sbdb.model.BugInfo;
import com.oracle.sbdb.model.Updates;
import com.oracle.sbdb.servlet.model.BugUpdates;

public class UpdatesBo {
	public static UpdatesBo INSTANCE = new UpdatesBo();
	
	private EngineerBo engineerBo = EngineerBo.INSTANCE;
	
	private static UpdatesDao updatesDao = new UpdatesDao();
	
	private static Map<Integer, List<BugUpdates>> map = new HashMap<Integer, List<BugUpdates>>();
	
	private UpdatesBo(){};
	
	public List<BugUpdates> selectbyUpdatesId(int bugId){
		List<BugUpdates> res = null;
		if(map.containsKey(bugId)){
			res = map.get(bugId);
		}else{
			List<Updates> updatesList = updatesDao.selectbyBugId(bugId);
			Collections.sort(updatesList, new Comparator<Updates>() {
				public int compare(Updates updates1, Updates updates2) {
					if(updates1.getUpdate_time().before(updates2.getUpdate_time())){
						return 1;
					}else if(updates1.getUpdate_time().after(updates2.getUpdate_time())){
						return -1;
					}else{
						return 0;
					}
				}
			});
			res = new ArrayList<BugUpdates>();
			for(Updates updates : updatesList){
				res.add(convert2BugUpdates(updates));
			}
		}
		return res;
	}
	
	public BugUpdates selectLatestUpdate(int bugId){
		BugUpdates res = null;
		Updates updates = updatesDao.selectLatestUpdate(bugId);
		if(updates != null){
			res = convert2BugUpdates(updates);
		}
		return res;
	}
	
	public boolean addUpdates(BugUpdates bugUpdates){
		boolean res = false;
		
		Updates updates = convert2Updates(bugUpdates);
		if(updates != null){
			BugInfo bugInfo = BugInfoBo.INSTANCE.selectBugInfobyID(updates.getBug_id());
			if(bugInfo != null){
				updatesDao.insert(updates);
				res = true;
				
				if(map.containsKey(bugUpdates.getBugID())){
					map.get(bugUpdates.getBugID()).add(bugUpdates);
				}
			}
		}
		
		return res;
	}
	
	private BugUpdates convert2BugUpdates(Updates updates){
		BugUpdates bugUpdates = new BugUpdates();
		int bugID = updates.getBug_id();
		String content = updates.getUpdate_content();
		int engineerId = updates.getUpdate_engineer_id();
		Boolean hidden = updates.getUpdate_hiden();
		Date dateTime = updates.getUpdate_time();
		
		String engineer = engineerBo.getEngineerGIdbyId(engineerId);
		String time = dateTime.toGMTString();
		
		bugUpdates.setBugID(bugID);
		bugUpdates.setContent(content);
		bugUpdates.setEngineer(engineer);
		bugUpdates.setHidden(hidden);
		bugUpdates.setTime(time);
		
		return bugUpdates;
	}
	
	private Updates convert2Updates(BugUpdates bugUpdates){
		int bugID = bugUpdates.getBugID();
		String content = bugUpdates.getContent();
		String engineerGID = bugUpdates.getEngineer();
		boolean hidden = bugUpdates.getHidden();
		String dateString = bugUpdates.getTime();
		
		int engineerID = engineerBo.getEngineerIdbyGId(engineerGID);
		Date date = new Date(Long.parseLong(dateString));
		Updates updates = new Updates(bugID, content, engineerID, hidden, date);
		return updates;
		
	}

	public boolean insert(BugUpdates bugUpdates) {
		boolean res = false;
		Updates updates = convert2Updates(bugUpdates);
		if(updates != null){
			BugInfo bugInfo = BugInfoBo.INSTANCE.selectBugInfobyID(updates.getBug_id());
			if(bugInfo != null){
				updatesDao.insert(updates);
				res = true;
			}
		}
		return res;
	}
	
}
